package org.android.kumoh_ing;

import java.io.IOException;
import java.io.UnsupportedEncodingException;
import java.util.ArrayList;

import org.apache.http.HttpEntity;
import org.apache.http.NameValuePair;
import org.apache.http.client.ClientProtocolException;
import org.apache.http.client.HttpClient;
import org.apache.http.client.ResponseHandler;
import org.apache.http.client.entity.UrlEncodedFormEntity;
import org.apache.http.client.methods.HttpPost;
import org.apache.http.impl.client.BasicResponseHandler;
import org.apache.http.impl.client.DefaultHttpClient;
import org.apache.http.message.BasicNameValuePair;


public class OtherDBTransfer {
	
	public String setGroupToDB(String gno, String u_id, String u_gmail, String u_phone) throws ClientProtocolException, IOException {
 		// TODO Auto-generated method stub
		HttpPost request = makeHttpPost(gno, u_id, u_gmail, u_phone, "http://14.63.225.125/insertUSER.php");
		HttpClient client = new DefaultHttpClient();
		ResponseHandler<String> reshandler = new BasicResponseHandler();
		String result = client.execute(request, reshandler);
		return result;
	}
	//총 그룹개수
	public int getGroupCount(String u_id)throws ClientProtocolException, IOException {
		HttpPost request = makeHttpPost(u_id, "http://14.63.225.125/getGroupCount.php");
		HttpClient client = new DefaultHttpClient();
		ResponseHandler<String> reshandler = new BasicResponseHandler();
		int result = Integer.parseInt(client.execute(request, reshandler).trim());
		return result;
	}
	//특정그룹의 맴버수
	public int getGroupMemberCount(String gno)throws ClientProtocolException, IOException {
		HttpPost request = makeHttpPost_gno(gno, "http://14.63.225.125/getGroupMemberCount.php");
		HttpClient client = new DefaultHttpClient();
		ResponseHandler<String> reshandler = new BasicResponseHandler();
		int result = Integer.parseInt(client.execute(request, reshandler).trim());
		return result;
	}
	public String getGroupMemberEmail(String gno)throws ClientProtocolException, IOException {
		HttpPost request = makeHttpPost_gno(gno, "http://14.63.225.125/getGroupMemberEmail.php");
		HttpClient client = new DefaultHttpClient();
		ResponseHandler<String> reshandler = new BasicResponseHandler();
		String result = client.execute(request, reshandler);
		return result;
	}
	public String getPhoneFromID(String u_no)throws ClientProtocolException, IOException {
		HttpPost request = makeHttpPost(u_no, "http://14.63.225.125/getPhoneFromID.php");
		HttpClient client = new DefaultHttpClient();
		ResponseHandler<String> reshandler = new BasicResponseHandler();
		String result = client.execute(request, reshandler);
		return result;
	}

	private HttpPost makeHttpPost(String $gno, String $u_id, String $u_gmail, String $u_phone, String $url) {

		// TODO Auto-generated method stub

		HttpPost request = new HttpPost($url);

		ArrayList<NameValuePair> nameValue = new ArrayList<NameValuePair>();
		
		nameValue.add(new BasicNameValuePair("gno", $gno));
		nameValue.add(new BasicNameValuePair("u_id", $u_id));
		nameValue.add( new BasicNameValuePair("u_gmail", $u_gmail ));
		nameValue.add( new BasicNameValuePair("u_phone", $u_phone ));
		request.setEntity(makeEntity(nameValue));
		return request; 
	}
	
	private HttpPost makeHttpPost(String $u_id, String $url) {

		// TODO Auto-generated method stub

		HttpPost request = new HttpPost($url);

		ArrayList<NameValuePair> nameValue = new ArrayList<NameValuePair>();
		
		nameValue.add(new BasicNameValuePair("u_id", $u_id));
		request.setEntity(makeEntity(nameValue));
		return request; 
	}
	private HttpPost makeHttpPost_gno(String $gno, String $url) {

		// TODO Auto-generated method stub

		HttpPost request = new HttpPost($url);

		ArrayList<NameValuePair> nameValue = new ArrayList<NameValuePair>();
		
		nameValue.add(new BasicNameValuePair("gno", $gno));
		request.setEntity(makeEntity(nameValue));
		return request; 
	}
	private HttpEntity makeEntity(ArrayList<NameValuePair> $nameValue) {

		HttpEntity result = null;

		try {

			result = new UrlEncodedFormEntity($nameValue);

		} catch (UnsupportedEncodingException e) {

			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		return result;
	}
}
